<?php
/*  
==================================================================
0. PENILAIAN
==================================================================
*/

function list_penilaian(){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=penilaian_delete&id=\'+id;
			}
			return false;
		}
	');

	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'Periode'=>array('style'=>'border-bottom:2px solid;width:85%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);

	$query = "SELECT * FROM penilaian ORDER BY tanggal DESC ";
	$result = my_query($query);
	$i = 0;
	$row=array();
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
			'href'=>'index.php?com=unsur_evaluasi&task=penilaian_create&id='.$ey['penilaian_id'],
			'rel'=>'facebox',
			'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		$detailproperty = array(
			'href'=>'index.php?com=unsur_evaluasi&task=list_unsur_evaluasi&penilaian_id='.$ey['penilaian_id'],
			'title'=>'Unsur Evaluasi'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$query="SELECT penilaian_id FROM unsur_evaluasi 
			WHERE penilaian_id=".$ey['penilaian_id'] ;
		if (my_num_rows( my_query($query)) >0){
			$delete_button = button_icon( 'b_drop_off.png' , ''  );
		}else{
			$deleteproperty = array(
				'href'=>'javascript:;',
				'title'=>'Delete',
				'onclick'=>'confirmDelete('.$ey['penilaian_id'].')',
			);
			$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		}	
 
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'periode'	=> position_text_align ( date("m-Y", strtotime($ey['tanggal'] ." 00:00:00" ) ), 'left'), 
			'operasi'=> position_text_align(  $edit_button.' '.$detail_button.' '.$delete_button, 'right'),  
		);
		 
	}
	$datas = table_rows($row);  
 
	return table_builder($header , $datas ,  3 , false );
} 


function form_penilaian_create($id = 0){

	$view = form_header( "penilaian" , "penilaian"  );
	if($id>0){
		$fields = my_get_data_by_id('penilaian','penilaian_id',$id);
		$month = date('m',strtotime($fields['tanggal']));
		$year = date('Y',strtotime($fields['tanggal']));	
	}else{
		$month = date('m');
		$year = date('Y');
	} 	
	$bulans  = array(
		'01'=>'01', 
		'02'=>'02', 
		'03'=>'03', 
		'04'=>'04', 
		'05'=>'05', 
		'06'=>'06', 
		'07'=>'07', 
		'08'=>'08', 
		'09'=>'09', 
		'10'=>'10', 
		'11'=>'11', 
		'12'=>'12', 
	);
	$bulan = array(
		'name'=>'bulan',
		'value'=>isset($_POST['bulan']) ? $_POST['bulan']: $month  
	);
	$bulan_form = form_dropdown( $bulan,$bulans);
	
	$tahun = array(
		'name'=>'tahun','size'=>5,
		'value'=>isset($_POST['tahun']) ? $_POST['tahun']: $year
	); 
	$tahun_form = form_dynamic($tahun );

	$view .= form_field_display( $bulan_form.' '. $tahun_form  , "Periode"    );
	if($id == 0)
	$view .= form_field_display( '<input type="checkbox" name="auto" checked="checked" /> Unsur dan element evaluasi sama dengan periode lalu'  , ""    );
	
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
} 
	 
function form_penilaian_submit($id=0){
	$tanggal = $_POST['tahun'].'-'.$_POST['bulan'].'-01';
	$datas = array(
		'tanggal'=> my_type_data_str($tanggal),
		'datetime_added'=>my_type_data_function('NOW()') 		
	);
	
	if($id > 0){
		return my_update_record( 'penilaian' , 'penilaian_id' , $id , $datas);
	}

	$query = "SELECT penilaian_id FROM penilaian ORDER BY penilaian_id DESC LIMIT 1 ";
	$result = my_query($query);
	$penilaian_id = my_insert_record( 'penilaian' , $datas );
	if(isset($_POST['auto'])){ 
		$row = my_fetch_array($result);
		$current_penilaian_id = $row['penilaian_id']; 
		$queryx = "SELECT * FROM unsur_evaluasi WHERE penilaian_id = {$current_penilaian_id}";
		$resultx = my_query($queryx);
		while($rowx = my_fetch_array($resultx)){
			$datasx = array(
				'penilaian_id'			=> my_type_data_int($penilaian_id),
				'nama_unsur_evaluasi' 	=> my_type_data_str($rowx['nama_unsur_evaluasi']),
				'pimpinan' 				=> my_type_data_str($rowx['pimpinan']),
				'pelaksana'				=> my_type_data_str($rowx['pelaksana']),
			);
			$unsur_evaluasi_id = my_insert_record('unsur_evaluasi',$datasx);
			
			$queryy = "SELECT * FROM evaluasi WHERE unsur_evaluasi_id = {$rowx['unsur_evaluasi_id']}";
			$resulty = my_query($queryy);
			while($rowy = my_fetch_array($resulty)){
				$datasy = array(
					'nama_evaluasi'		=> my_type_data_str($rowy['nama_evaluasi']), 	
					'unsur_evaluasi_id' => my_type_data_int($unsur_evaluasi_id),	
					'uraian'			=> my_type_data_str($rowy['uraian']),
				);
				my_insert_record('evaluasi',$datasy);
			}
		}
	}
	return $penilaian_id;
}

function form_penilaian_validate($penilaian_id){
	$errsubmit = false;
	$err = array();
	
	if( (int)$_POST['bulan'] == 0 ){
		$errsubmit = true;
		$err[] = "Bulan belum di isi";	
	}
	
	if( (int)$_POST['tahun'] == 0 ){
		$errsubmit = true;
		$err[] = "Tahun belum di isi";	
	}
    
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function delete_penilaian($penilaian_id){
	$query = "DELETE FROM penilaian WHERE penilaian_id={$penilaian_id}";
	return my_query($query);
}


/*  
==================================================================
1. UNSUR EVALUASI
==================================================================
*/

function list_unsur_evaluasi($penilaian_id){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=unsur_evaluasi_delete&id=\'+id;
			}
			return false;
		}
	');

	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'Unsur Evaluasi'=>array('style'=>'border-bottom:2px solid;width:65%'),  
		'Pimpinan'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Pelaksana'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);

	$query = "SELECT * FROM unsur_evaluasi WHERE penilaian_id={$penilaian_id} ";
	$result = my_query($query);
	$i = 0;
	$row=array();
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
			'href'=>'index.php?com=unsur_evaluasi&task=unsur_evaluasi_create&penilaian_id='.$penilaian_id.'&id='.$ey['unsur_evaluasi_id'],
			'rel'=>'facebox',
			'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		$detailproperty = array(
			'href'=>'index.php?com=unsur_evaluasi&task=list_evaluasi&uid='.$ey['unsur_evaluasi_id'],
			'title'=>'Evaluasi'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$query="SELECT evaluasi_id FROM evaluasi 
			WHERE unsur_evaluasi_id=".$ey['unsur_evaluasi_id'] ;
		if (my_num_rows( my_query($query)) >0){
			$delete_button = button_icon( 'b_drop_off.png' , ''  );
		}else{
			$deleteproperty = array(
				'href'=>'javascript:;',
				'title'=>'Delete',
				'onclick'=>'confirmDelete('.$ey['unsur_evaluasi_id'].')',
			);
			$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		}	
 
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'nama'=>  $ey['nama_unsur_evaluasi'] ,   
			'pimpinan'=>  $ey['pimpinan'] ,   
			'pelaksana'=>  $ey['pelaksana'] ,   
			'operasi'=> position_text_align(  $edit_button.' '.$detail_button.' '.$delete_button, 'right'),  
		);
		 
	}
	$datas = table_rows($row);   
	$penilaians = my_get_data_by_id('penilaian','penilaian_id',$penilaian_id); 
	
	$view ='<table width="90%" cellspacing="0" cellpadding="4"><tr>'; 
	$view .= form_header( "Evaluasi" , "Evaluasi"  );
	$view .= form_field_display( '<span class="label_form">'.date("m-Y", strtotime($penilaians['tanggal'] ." 00:00:00" ) ).'</span>', "<b>Periode</b>"); 
	
	return $view.table_builder($header , $datas ,  3 , false );
} 

function form_unsur_evaluasi_create($penilaian_id, $id = 0){
	my_set_code_js('
	$(function () { // this line makes sure this code runs on page load
		$(\'.checkall\').click(function () {
			$(this).parents(\'pg:eq(0)\').find(\':checkbox\').attr(\'checked\', this.checked);
		});
	});');

	$view = form_header( "unsur_evaluasi" , "unsur_evaluasi"  );
 	$fields = my_get_data_by_id('unsur_evaluasi','unsur_evaluasi_id',$id);
	 
	$nama = array(
			'name'=>'nama_unsur_evaluasi',
			'value'=>( isset($_POST['nama_unsur_evaluasi']) ? $_POST['nama_unsur_evaluasi'] : $fields['nama_unsur_evaluasi'] ),
			'id'=>'nama_unsur_evaluasi',
			'type'=>'text',
			'size'=>'45'
			);
	$form_field_nama = form_dynamic($nama);
	$view .= form_field_display( $form_field_nama   , "Nama Unsur Evaluasi");

	if($fields['pimpinan']=='Y'){ 
		$pimpinan = array(
			'name'=>'pimpinan',
			'id'=>'pimpinan',
			'type'=>'checkbox',
			'checked'=>'checked'
			
		);
	}else{
		$pimpinan = array(
			'name'=>'pimpinan',
			'id'=>'pimpinan',
			'type'=>'checkbox',
		); 
	}
	$form_pimpinan = form_dynamic($pimpinan).'Pimpinan';
	
	if($fields['pelaksana']=='Y'){ 
		$pelaksana = array(
			'name'=>'pelaksana',
			'id'=>'pelaksana',
			'type'=>'checkbox',
			'checked'=>'checked'
			
		);
	}else{
		$pelaksana = array(
			'name'=>'pelaksana',
			'id'=>'pelaksana',
			'type'=>'checkbox',
		); 
	}
	$form_pelaksana = form_dynamic($pelaksana).'Pelaksana';
	$form_komponen_status ='<pg>
		'.$form_pimpinan.' &nbsp; '.$form_pelaksana.' &nbsp; <input type="checkbox" class="checkall"> Check all<pg>
		';
	$view .= form_field_display( $form_komponen_status , "Keterangan" );	
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
} 
	 
function form_unsur_evaluasi_submit($penilaian_id, $id=0){
	$pimpinan = isset($_POST['pimpinan']) ? 'Y': 'T';
	$pelaksana = isset($_POST['pelaksana']) ? 'Y': 'T';
	$datas = array(
		'penilaian_id'=> my_type_data_int($penilaian_id),
		'nama_unsur_evaluasi'=>my_type_data_str( $_POST['nama_unsur_evaluasi'] ),
		'pimpinan'	 =>my_type_data_str($pimpinan),			
		'pelaksana'	 =>my_type_data_str($pelaksana),			
	);
	if($id > 0){
		return my_update_record('unsur_evaluasi' ,'unsur_evaluasi_id' , $id , $datas);
	}
	
	return my_insert_record('unsur_evaluasi' , $datas);
} 

function form_unsur_evaluasi_validate($penilaian_id, $unsur_evaluasi_id){
	$errsubmit = false;
	$err = array();
	 
	$nama =  trim($_POST["nama_unsur_evaluasi"]); 
	if( $nama == ''){
		$errsubmit = true;
		$err[] = "Nama unsur evaluasi belum diisi";
	}

	$query="SELECT unsur_evaluasi_id FROM unsur_evaluasi 
		WHERE nama_unsur_evaluasi='".$nama.
		"' AND unsur_evaluasi_id<>".$unsur_evaluasi_id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Nama unsur evaluasi sudah ada";	
	}
    
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function delete_unsur_evaluasi($unsur_evaluasi_id){
	$query = "DELETE FROM unsur_evaluasi WHERE unsur_evaluasi_id ={$unsur_evaluasi_id} ";
	return my_query($query);
}	

/*  
==================================================================
2. Evaluasi
==================================================================
*/

function list_evaluasi($uid = 0){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=evaluasi_delete&id=\'+id;
			}
			return false;
		}
	');
	$uid = (int) $uid;
	if($uid == 0)my_direct($_SERVER['HTTP_REFERER']);
		$url = 'index.php?com=unsur_evaluasi&task=delete_evaluasi&id=';
		remove_data_load_js( $url );
		$header = array(
			'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
			'Evaluasi'=>array('style'=>'border-bottom:2px solid;width:35%'),  
			'Uraian Hasil Kerja'=>array('style'=>'border-bottom:2px solid;width:50%'),  
			' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
 
	$query = "SELECT * FROM evaluasi WHERE unsur_evaluasi_id ={$uid}";
 	$result = my_query($query);
	$i = 0;
	$row=array();
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
			'href'=>'index.php?com=unsur_evaluasi&task=evaluasi_create&uid='.$uid.'&id='.$ey['evaluasi_id'],
			'rel'=>'facebox',
			'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
			'href'=>'javascript:;',
			'title'=>'Delete',
			'onclick'=>'confirmDelete('.$ey['evaluasi_id'].')',
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'nama'=>  $ey['nama_evaluasi'] ,    
			'uraian'=>  $ey['uraian'] ,    
			'operasi'=> position_text_align(  $edit_button.' '.$delete_button, 'right'),  
		);
		 
	}
	
	$datas = table_rows($row); 
	$unsur_evaluasi = my_get_data_by_id('unsur_evaluasi','unsur_evaluasi_id',$uid); 
	$penilaians = my_get_data_by_id('penilaian','penilaian_id',$unsur_evaluasi['penilaian_id']); 
	
	$view ='<table width="90%" cellspacing="0" cellpadding="4"><tr>'; 
	$view .= form_header( "Evaluasi" , "Evaluasi"  );
	$view .= form_field_display( '<span class="label_form">'.date("m-Y", strtotime($penilaians['tanggal'] ." 00:00:00" ) ).'</span>', "<b>Periode</b>"); 
	$view .= form_field_display( '<span class="label_form">'.$unsur_evaluasi['nama_unsur_evaluasi'].'</span>', "<b>Unsur Evaluasi</b>"); 
	return  $view. table_builder($header , $datas ,  3 , false );
}

function form_evaluasi_create($uid, $id = 0){
	$view = form_header( "unsur_evaluasi" , "unsur_evaluasi"  );
 	$fields = my_get_data_by_id('evaluasi','evaluasi_id',$id);
	$unsur_evaluasi = my_get_data_by_id('unsur_evaluasi','unsur_evaluasi_id',$uid); 

	$nama = array(
			'name'=>'nama_evaluasi',
			'value'=>( isset($_POST['nama_evaluasi']) ? $_POST['nama_evaluasi'] : $fields['nama_evaluasi'] ),
			'id'=>'nama_evaluasi',
			'type'=>'text',
			'size'=>'45'
			);
	$form_field_nama = form_dynamic($nama);
	$view .= form_field_display( $form_field_nama   , "Nama Evaluasi");	
	
	$c = array(
		'name'=>'uraian',
		'value'=>( isset($_POST['uraian']) ? $_POST['uraian'] : $fields['uraian'] ),
		'id'=>'uraian',  
		'rows'=>'3',
		'size'=>'45'
	);
	$form_field_nama = form_textarea($c);
	$view .= form_field_display( $form_field_nama   , "Uraian Hasil Kerja");

	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
} 
	 
function form_evaluasi_submit($uid , $id =0){
	$datas = array(
		'unsur_evaluasi_id'=>my_type_data_int($uid),	
		'nama_evaluasi'=>my_type_data_str( $_POST['nama_evaluasi'] ),
		'uraian'=>my_type_data_str( $_POST['uraian'] ),
	);
	
	if($id > 0){
		return my_update_record('evaluasi' ,'evaluasi_id' , $id , $datas);
	}
	return my_insert_record('evaluasi' , $datas);
} 

function form_evaluasi_validate($evaluasi_id){
	$errsubmit = false;
	$err = array();

	$nama =  trim($_POST["nama_evaluasi"]); 
	if( $nama == ''){
		$errsubmit = true;
		$err[] = "Nama evaluasi belum diisi";
	}

	$query="SELECT evaluasi_id FROM evaluasi 
		WHERE nama_evaluasi='".$nama.
		"' AND evaluasi_id<>".$evaluasi_id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Nama evaluasi sudah ada";	
	}
	
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function delete_evaluasi($evaluasi_id){
	$query = "DELETE FROM evaluasi WHERE evaluasi_id ={$evaluasi_id} ";
	return my_query($query);
}	
